#include "namemap.h"
#include <string.h>
#include <stdio.h>

struct NameListType {
	char content[64];
	struct NameListType* next;
};

struct NameListType* head = 0;

void clearNamemap() {
	struct NameListType* current, *next;
	current = head;
	while (current != 0) {
		next = current->next;
		free(current);
		current = next;
	}
	head = 0;
}

int nameToId(char * name) {
	int i = 0;
	struct NameListType* current = head, *prev = 0;
	while (current != 0) {
		if (strcmp(current->content, name) == 0) {
			return i;
		}
		prev = current;
		current = current->next;
		i++;
	}
	if (prev == 0) {
		head = malloc(sizeof(struct NameListType));
		strcpy(head->content, name);
		head->next = 0;
	} else {
		prev->next = malloc(sizeof(struct NameListType));
		strcpy(prev->next->content, name);
		prev->next->next = 0;
	}
	return i;
}

void idToName(int id, char * name) {
	int i = 0;
	name[0] = '\0';
	struct NameListType* current = head;
	for (i = 0; i < id; i++) {
		if (current == 0)
			return;
		current = current->next;
	}
	if (current == 0)
		return;
	strcpy(name, current->content);
}

